using System.Text;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;

namespace StudentVideo.Client.Components.CuIcon;

public partial class CuIcon: ComponentBase {
   [Parameter] public required CuIconEnum IconType { get; set; }

   /// <summary> 方形尺寸 </summary>
   [Parameter] public double Square { get; set; } = 24;
   /// <summary> 宽度(高优先级) </summary>
   [Parameter] public double? Width { get; set; } = null;
   /// <summary> 高度(高优先级) </summary>
   [Parameter] public double? Height { get; set; } = null;

   /// <summary> svg 容器宽度 </summary>
   [Parameter] public double? ViewBoxWidth { get; set; } = 48;
   /// <summary> svg 容器高度 </summary>
   [Parameter] public double? ViewBoxHeight { get; set; } = 48;

   /// <summary> 主要颜色 </summary>
   [Parameter] public string MainColor { get; set; } = "#333";

   /// <summary> 其他颜色 </summary>
   [Parameter] public List<string> OtherColor { get; set; } = ["#333", "#333", "#333"];

   [Parameter] public string Padding { get; set; } = string.Empty;

   [Parameter] public string Id { get; set; } = string.Empty;
   [Parameter] public string Class { get; set; } = string.Empty;
   [Parameter] public EventCallback<MouseEventArgs> OnClick { get; set; }

   private string svg = string.Empty;

   protected override void OnParametersSet() {
      base.OnParametersSet();

      Width ??= Square;
      Height ??= Square;

      if (OtherColor.Count < 3) {
         for (int i = 0; i < 3 - OtherColor.Count; i++) {
            OtherColor.Add("#333");
         }
      }

      // 尺寸替换
      if (!CuIconDic.Types.TryGetValue(IconType, out var str)) {
         throw new Exception("未找到对应的 svg");
      }

      StringBuilder builder = new(str);

      builder.Replace("{Width}", Width.ToString());
      builder.Replace("{Height}", Height.ToString());
      builder.Replace("{ViewBoxWidth}", ViewBoxWidth.ToString());
      builder.Replace("{ViewBoxHeight}", ViewBoxHeight.ToString());
      builder.Replace("{MainColor}", MainColor);

      int index = 0;
      foreach (var color in OtherColor) {
         builder.Replace($"OtherColor_{index}", color);

         index++;
      }

      svg = builder.ToString();
   }

   private async Task HandleClick(MouseEventArgs args) {
      if (OnClick.HasDelegate) {
         await OnClick.InvokeAsync(args);
      }
   }
}
